package com.yonyou.gmmc.service.wechatpush.controller;

import static org.marketing.common.constants.MessagePushConstant.WX_MSG_PROP_COLOR;

import java.util.Date;

import org.marketing.common.constants.BusinessConstant;
import org.marketing.common.constants.MessagePushConstant;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocol;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolMessageContent;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolMessageTemplateContent;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolReceiver;
import org.marketing.common.entity.wechatpushmessage.WeChatMessageProtocolSender;
import org.marketing.common.event.BizEvent;
import org.marketing.common.event.EventPointsChangedDTO;
import org.marketing.common.event.enums.EventConstant.EventBizStep;
import org.marketing.common.event.enums.EventConstant.EventBizType;
import org.marketing.common.util.DateUtil;
import org.marketing.common.util.MapConverUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.util.concurrent.ListeningExecutorService;
import com.yonyou.gmmc.service.wechatpush.common.ApiVersionConsts;
import com.yonyou.gmmc.service.wechatpush.event.handler.PointChangeEventHandler;
import com.yonyou.gmmc.service.wechatpush.service.TokenService;

import io.swagger.annotations.Api;


@RestController
@RequestMapping(value = ApiVersionConsts.BASE_PATH+ApiVersionConsts.VERSION+"/wechatApiService" )
public class WechatApiServiceController {
	@Autowired
	private TokenService tokenService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ListeningExecutorService executorService;
//    @Autowired
//    private BaseService mqInterface;
	
	@RequestMapping(value = "/getOpenid/{code}", method = RequestMethod.GET,consumes = "application/json;UTF-8")
	public String getOpenidByCode(@PathVariable("code") String code) {
		return tokenService.getOpenidByCode(code);
	}
	@RequestMapping(value = "/getToken", method = RequestMethod.GET,consumes = "application/json;UTF-8")
	public String getToken() {
		return tokenService.getAccessToken();
	}

	@RequestMapping(value = "/testsend", method = RequestMethod.GET)
	public void testsend() throws Exception {
		EventPointsChangedDTO o=new EventPointsChangedDTO();
		o.setAfterPoints(100l);
		o.setBeforePoints(1000l);
		o.setChangePoints(900l);
		o.setUserId(1512);
		o.setReason("buy car");
		BizEvent event=new BizEvent();
		event.setEventBizType(EventBizType.COMMON);
		event.setEventBizStep(EventBizStep.COMMON_POINTS_CHANGED);
		event.setEventKey("CD110000111");
		event.setEventData(MapConverUtil.PO2Map(o));
		PointChangeEventHandler test=new PointChangeEventHandler();
		test.executorService=executorService;
		test.handleing(event);
		//mqInterface.sendEvent2MQ("exchange2", "1", "1", "2017-03-03", "1", JSON.toJSON(event).toString());
		
//        WeChatMessageProtocol messageProtocol = getDemoWXTemplate();
//        this.rabbitTemplate.convertAndSend(MessagePushConstant.WECHAT_QUEUE, messageProtocol);
	}
	
    public static WeChatMessageProtocol getDemoWXTemplate(){
        WeChatMessageProtocol messageProtocol = new WeChatMessageProtocol();

        WeChatMessageProtocolReceiver receiver = new WeChatMessageProtocolReceiver();
        receiver.setDetail(true);
        receiver.setPlatform(MessagePushConstant.PUSH_PLATFORM.WC_PUBLIC);
        //openID 需要通过接口调用获取
//        receiver.addDetailList("omLPIwCuHiobN1XE09Sf_3G_LXiQ");//陆臻
        //receiver.addDetailList("omLPIwMuKtW70hklrdQDPLVnXZkU");//刘俊
        receiver.addDetailList("omLPIwITFcilUn_u5EPrINDwFlPg");//陈春周
        receiver.addDetailList("omLPIwMuQ6ZUPPF2zBM4J-89Y0BQ");//钱劲松
        WeChatMessageProtocolMessageContent message = new WeChatMessageProtocolMessageContent();
        //试驾预约
        message.setContent("TM00629");
        
        message.setSourceType(BusinessConstant.SOURCE_TYPE_WECHAT);

        //
        WeChatMessageProtocolMessageTemplateContent content1 = new WeChatMessageProtocolMessageTemplateContent();
        content1.setKey("first");content1.setValue("您的试驾预约已安排");
        content1.addExtraMap(WX_MSG_PROP_COLOR,"#FF0000");
        message.addTemplateConentList(content1);

        WeChatMessageProtocolMessageTemplateContent content2 = new WeChatMessageProtocolMessageTemplateContent();
        content2.setKey("keynote1");content2.setValue("宝马XYZ");
        message.addTemplateConentList(content2);

        WeChatMessageProtocolMessageTemplateContent content3 = new WeChatMessageProtocolMessageTemplateContent();
        content3.setKey("keynote2");content3.setValue(DateUtil.formateDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
        message.addTemplateConentList(content3);

        WeChatMessageProtocolMessageTemplateContent content4 = new WeChatMessageProtocolMessageTemplateContent();
        content4.setKey("keynote3");content4.setValue("ABC顾问");
        message.addTemplateConentList(content4);

        
        WeChatMessageProtocolMessageTemplateContent content5 = new WeChatMessageProtocolMessageTemplateContent();
        content5.setKey("remark");content5.setValue("我是备注Test");
        message.addTemplateConentList(content5);

        WeChatMessageProtocolSender sender = new WeChatMessageProtocolSender();
        sender.setOriginApplicationHost("host1");
        sender.setOriginApplicationName("app1");
        sender.setClientAppId("wxb85ed633e428bafc");
        sender.setDealerCode("270001");
        messageProtocol.setMessage(message);
        messageProtocol.setReceiver(receiver);
        messageProtocol.setSender(sender);
        messageProtocol.setPushChannel(MessagePushConstant.PUSH_CHANNEL.WEBCHAT);

        return messageProtocol;
    }
}
